#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
#define rep(i,l,r) for(int i=(l),i##end=(r);i<=i##end;++i)
#define per(i,r,l) for(int i=(r),i##end=(l);i>=i##end;--i)
#define ll long long
#define int ll
#define double long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define popcnt __builtin_popcount
#define rbtree(way) tree<way,null_type,less<way>,rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
const int maxn=1e6+10,maxm=1e6+10,mod=998244353,inf=INT_MAX;
inline int ksm(int x,int k,int mod=mod){
	int ans=1;
	for(x%=mod;k;k>>=1,x=x*x%mod) if(k&1) ans=ans*x%mod;
	return ans;
}

int n,ans,f[maxn],t[maxn];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}

signed main(){
	freopen("pairint.in","r",stdin),freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n,ans=2*n;
	rep(i,1,2*n) f[i]=i;
	rep(i,1,n){
		int u,v;
		cin>>u>>v;
		if(find(u)==find(v)) t[find(u)]=1;
		else t[find(v)]|=t[find(u)],f[find(u)]=find(v);
	}
	rep(i,1,2*n) if(f[i]==i) ans+=t[i]-1;
	cout<<ans;
	return 0;
}
